package ch.zarzu.champions.updater;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import java.util.*;
import java.util.zip.*;

import javax.swing.*;

public class Main {
	private JFrame frame;
	private JLabel label2;
	private String app_path;
	
	private void startUpdate() {
	   
	    Enumeration entries;
	    ZipFile zipFile;

	    try {
	      zipFile = new ZipFile(app_path + "updates/new.zip");

	      entries = zipFile.entries();

	      while(entries.hasMoreElements()) {
	        ZipEntry entry = (ZipEntry)entries.nextElement();

	        if(entry.isDirectory()) {
	          // Assume directories are stored parents first then children.
	          (new File(app_path + entry.getName())).mkdir();
	          continue;
	        }
	        
	        	
	        label2.setText("Extracting " + entry.getName());
	        copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(app_path + entry.getName())));
	      }

	      zipFile.close();
	    } catch (IOException e) {
	      e.printStackTrace();
	      String output = e.toString();
			try {
				FileWriter file_writer = new FileWriter(app_path + "data/error.txt");
				file_writer.write(output);
				file_writer.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
	    }
	    
	    try {
    		Runtime.getRuntime().exec(app_path + "championBuilder.exe");
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	
    	File f = new File(app_path + "updates/new.zip");
    	f.delete();
    	System.exit(0);
	}

	public static final void copyInputStream(InputStream in, OutputStream out)
	throws IOException
	{
	  byte[] buffer = new byte[1024];
	  int len;
	
	  while((len = in.read(buffer)) >= 0)
	    out.write(buffer, 0, len);
	
	  in.close();
	  out.close();
	}

	
    private void createAndShowGUI() {
        frame = new JFrame("championBuilder Updater");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setResizable(false);
        
        app_path = AppPath.get();
        
        Container pane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
		pane.setLayout(layout);
		pane.setBackground(Color.BLACK);
        
        ImageIcon icon = new ImageIcon(app_path + "images/misc/updater.jpg");
		JLabel label = new JLabel(icon);
		label.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
		System.out.println(icon.getIconHeight());
        
		label2 = new JLabel("Searching file...");
        label2.setForeground(Color.WHITE);
        label2.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
        label2.setPreferredSize(new Dimension(icon.getIconWidth() - 20, 20));
        
        pane.add(label);
        pane.add(label2);
        layout.putConstraint(SpringLayout.WEST, label2, 10, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, label2, 0, SpringLayout.SOUTH, label);
		layout.putConstraint(SpringLayout.SOUTH, pane, 0, SpringLayout.SOUTH, label2);
		layout.putConstraint(SpringLayout.EAST, pane, 0, SpringLayout.EAST, label);
		
		

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screen_size = toolkit.getScreenSize();
        int x = (screen_size.width - icon.getIconWidth())/2;
        int y = (screen_size.height - icon.getIconHeight() - 80)/2;
		
        frame.setLocation(x, y);
        
        //display the window
        frame.pack();
        frame.setVisible(true);
        
        startUpdate();
    }
    
	//main
    public static void main(String[] args) {
    	Main main = new Main();
        main.createAndShowGUI();
    }
}
